<?php

require_once( '../system/smarty.inc' );

require_once( '../system/tdao/Class_T_PATTERN.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_T_PATTERN,$login['staff_auth']);
if   ($login['staff_auth'] == 'G-6' || $login['staff_auth'] == 'G-7')  {
	
	header("Location: ./tpattern_shop.php");
	exit;
}


$dosearch = FALSE ;



$forms = $_SESSION['MSEARCH']['INS_SHOP'] ;

$dao = new Class_T_PATTERN;
$matrix_cd = $forms['matrix_cd'] ;
$dao->setTableNameWeek($matrix_cd);

$frmdatas = $_POST;
//$values = array();

//行列交換
foreach($frmdatas as $k => $v)
 {
	 foreach($v as $kk =>$vv){
	     $data[$kk][$k]= $vv;
	 }
 }
 
$patterns = $dao->getpatterns($forms['block_cd']);
$patttenFields =array();
foreach($patterns as $k => $v) {
	$patttenFields[$v["pattern_cd"]] = $v["pattern_name"];
}
$hdata = array();
$ddata = array();

foreach($data as $k => $v) {
	if (!empty($v["pper"])) {
		$vins_shop_shop_cd =$v["ins_shop_shop_cd"];
		$vins_shop_cd      = $dao->quote($v["ins_shop_cd"]);
		
		$hdata[] = array("ins_shop_shop_cd"=> $vins_shop_shop_cd,"shop_cd"=> $v["shop_cd"], "ins_shop_cd"=>$vins_shop_cd, "pper"=>$v["pper"], "pnums"=> $v["pnums"], "pmemo"=> $v["pmemo"]);
		$dateP = NULL;
		foreach($patterns as $kk => $vv) {
			if (!empty($v[$vv["col"]])) {
				$dateP[]  = array("pattern_cd"=> $vv["pattern_cd"], "pattern_name"=> $vv["pattern_name"], "pnums"=> $v[$vv["col"]]);
			}
		}
		
		$ddata[$vins_shop_shop_cd] = $dateP; 
	}
}
$msg = "データテックエラー";
$errmsg =  "";

$err     = new Class_ERROR("","");
$chk     = $dao->get_checks();
$errfields = array("ins_shop_shop_cd" => "キー","pper" => "部数パセント", "pnums"=> "部数", "pmemo"=> "備考"); 

foreach($hdata as $k => $v) {
	//ヘッダチェック
	foreach ($chk as $kcol => $chkval) {
		$errmsg  = $err->check( $v[$kcol], $chkval );
		
		if( !($err->clear) ){
			$errmsg = $errfields[$kcol].":". $errmsg ;
			break;
		}
	}
	if( $errmsg ){
		break;
	}

	//明細チェック
	foreach ($ddata[$v["ins_shop_shop_cd"]] as $key =>$vv) {

		$errmsg  = $err->check( $vv["pnums"], array( "NUM" ) );

		if( !($err->clear) ){
			$errmsg = $patttenFields[$vv["pattern_cd"]].":". $errmsg ;
			break;
		}
	}

	if( $errmsg ){
		break;
	}
}


if (empty($errmsg)) {
	try {
		//	print_r($hdata);
		$dao->begin_trans();
		$executeflg = TRUE;
		
		foreach($hdata as $k => $v) {
			
			if (!$dao->doSaveH($v))
			{
				$executeflg = FALSE;
				break;
			}
			
			if (!empty($ddata[$v["ins_shop_shop_cd"]])) {
				
				if (!$dao->doSaveD($dao->quote($v["ins_shop_shop_cd"]), $ddata[$v["ins_shop_shop_cd"]]))
				{
					$executeflg = FALSE;
					break;
				}
			}
		}
		
		if  ($executeflg) {
			$dao->commit_trans();
			$msg =  '更新が完了しました。';
		} else {
			$dao->rollback_trans();
			$msg =  '更新が失敗しました。';
		}

		
	} catch (Exception $e) {
		$dao->rollback_trans();
		$msg = 'ＤＢエラーで失敗しました。';
	}
}



$page["cnt"] = count($data);

//================AJAゾーンエリア==================
$login_auth = $login['staff_auth'];

if ($login_auth == 'G-2' ) {
	$area_cd   = $login['autoid'];
	$forms["sort"] = '2';
	$areauser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else if ($login_auth == 'G-3' ) {
	$zone_cd   = $login['autoid'];
	$forms["sort"] = '3';
	$cellsort = array();
	$zoneuser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else {
	// PLEASE_SELECT_ARR ;
	$zoneuser  = array();
	$zoneuser  = Class_M_OTHER::get_ajaeks_array(NULL, 3);
	$areauser = array();
	$areauser =  Class_M_OTHER::get_ajaeks_array(NULL, 2);
	$zone_cd = array();
}
//================AJAゾーンエリア==================

//==================================周
if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}
$smarty->assign('year4',  Class_PWD::get_years());

$week = Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );

$smarty->assign('week', $week );
//==================================周
if ( empty( $forms["sort"] ) ) {
	$forms["sort"] = "2";
}

  
if( isset($forms) )     $smarty->assign('forms', $forms);
if( isset($data) )      $smarty->assign('data', $data);
if( isset($patterns) )  $smarty->assign('pattern', $patterns);
if( isset($page) )      $smarty->assign('page', $page);


$aoki_area_zones = array();
//$aoki_area_zones[""] = "すべて";

foreach ($aoki_area_zone as $k => $v) {
	$aoki_area_zones[$k] = $v;
}
$smarty->assign('sorts', $aoki_area_zone );

if( isset($zoneuser) )    $smarty->assign('zoneuser', $zoneuser);
if( isset($areauser) )    $smarty->assign('areauser', $areauser);


/*
$ins_co = Class_M_OTHER::get_ins_co_array( PLEASE_SELECT_ARR );
$smarty->assign('ins_co', $ins_co );

$local= Class_M_OTHER::get_local_array( PLEASE_SELECT_ARR );
$smarty->assign('local', $local );
*/

$block = Class_M_OTHER::get_block_array( PLEASE_SELECT_ARR );
$smarty->assign('block', $block );


$isG2_3 = FALSE;
if   ($login['staff_auth'] == 'G-2' || $login['staff_auth'] == 'G-3')  {
	$isG2_3 = TRUE;
}
$smarty->assign('isG2_3', $isG2_3 );



$smarty->assign('msg', $msg );
$smarty->assign('errmsg', $errmsg );

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
//$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('tpattern_index.html');

?>